package logic

import (
	"context"
	"github.com/jinzhu/copier"
	"newwater/app/soil/cmd/rpc/internal/svc"
	"newwater/app/soil/cmd/rpc/pb"
	"newwater/app/user/cmd/rpc/user"
	"newwater/common/xerr"

	"github.com/tal-tech/go-zero/core/logx"
)

type FindSoilByUserNameLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewFindSoilByUserNameLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FindSoilByUserNameLogic {
	return &FindSoilByUserNameLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

var ErrUserNoExistsError = xerr.NewErrMsg("用户不存在")
//var ErrSoilNoExistsError = xerr.NewErrMsg("用户id查询失败")
func (l *FindSoilByUserNameLogic) FindSoilByUserName(in *pb.Username) (*pb.Soils, error) {
	queryuser, err := l.svcCtx.UserRpc.UserInfoByUserName(l.ctx, &user.UserName{
		Name: in.Username,
	})
	if err != nil {
		return nil, ErrUserNoExistsError
	}

	soils, err := l.svcCtx.WaterSoil.FindManyByUserId(queryuser.Id)
	if err != nil {
		return nil, err
	}
	var resp []*pb.SoilBody
	for _,value:= range soils {

		var soilbody pb.SoilBody
		_ = copier.Copy(&soilbody,value)
		resp = append(resp, &soilbody)

	}
	return &pb.Soils{
		Soils: resp,
	}, nil
}